// 一共五个状态
//可以说是5×4了这么多函数
// 用户、商品、动态、收藏、预定  
// 增 POST
// 删 DELETE
// 改 PUT
// 查 GET
import commentInter from '../data/Interface'
import { Register_tim, Login_tim } from '../TIM/actiontim'
import { Toast } from 'antd-mobile'
const errToast = (msg) => {
    Toast.info(`${msg}`, 1)
}
// 用户登陆函数
const denglu = (tel, password, props) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/user/slg'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_phone: `${tel}`, user_password: `${password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            if (res.code === 40001 || res.code === 40002 || res.code === 40003) {
                // errToast(res.msg)
                Toast.fail('Load failed !!!', 2);
            }
            else {
                // errToast(res.msg)
                Toast.success('Load success !!!', 2);
                // console.log(res.data)
                localStorage.setItem('user', JSON.stringify(res.data))
                localStorage.setItem('identity', 'user')
                dispatch({
                    type: 'ADD_UR',
                    login: res.data
                })
                Login_tim(tel, res.data.user_sig)
                setTimeout(() => {
                    props.history.push('/home')
                }, 1000);
            }
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 商家登录（果农）
const busdenglu = (tel, password, props) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/business'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_phone: `${tel}`, business_password: `${password}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            if (res.code === 40001 || res.code === 40002 || res.code === 40003) {
                // errToast(res.msg)
                Toast.fail('Load failed !!!', 2);
            }
            else {
                // errToast(res.msg)
                Toast.success('Load success !!!', 2);
                console.log(res.data[0])
                localStorage.setItem('user', JSON.stringify(res.data[0]))
                localStorage.setItem('identity', 'shop')
                dispatch({
                    type: 'ADD_UR',
                    login: res.data[0]
                })
                Login_tim(res.data[0].business_phone, res.data[0].business_sig)
                setTimeout(() => {
                    props.history.push('/bushome')
                }, 1000);
            }
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 商家注册
const zhecebus = (tel, password, password1) => {//注册函数
    let url = 'http://www.h5weixin.club:1024/api/business/register'
    // Register_tim(tel)
    fetch(url, {
        method: 'POST',
        mode: 'cors',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            business_phone: `${tel}`, business_password: `${password}`, password_confim: `${password1}`,
        })
    }).then((res) => {
        return res.json()
    }).then((res) => {
        // console.log(res)
        // localStorage.removeItem('usersig')
        if (res.code === 40004 || res.code === 40005 || res.code === 40006) {
            errToast(res.msg)
        } else {
            errToast(res.msg)
        }
    }).catch((err) => {
        console.log(err)
    })
}
// 用户注册
const zhece = (tel, password, password1) => {//注册函数
    let url = 'http://www.h5weixin.club:1024/api/user/register'
    // Register_tim(tel)
    fetch(url, {
        method: 'POST',
        mode: 'cors',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            user_phone: `${tel}`, user_password: `${password}`, password_confim: `${password1}`
        })
    }).then((res) => {
        return res.json()
    }).then((res) => {
        // localStorage.removeItem('usersig')
        if (res.code === 40004 || res.code === 40005 || res.code === 40006) {
            errToast(res.msg)
        } else {
            errToast(res.msg)
        }
    }).catch((err) => {
        console.log(err)
    })
}
// 查看用户
const getuserlist = () => {
    let url = 'http://www.h5weixin.club:1024/api/user'
    fetch(url, {
        method: 'GET',
        mode: 'cors',
        headers: {
            'Content-Type': 'application/json'
        }
    }).then((res) => {
        return res.json()
    }).then((res) => {
        console.log(res)
    })
}
//查看县城
const getCounty = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/county/'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((res) => {
            return res.json()
        })
            .then((res) => {
                // console.log(res)
                dispatch({
                    type: 'GET_COUNTY',
                    value: res.data
                })
            }).catch((err) => {
                console.log(err)
            })
    }
}
//获取所有商品
const getsplist = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity/'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // console.log(res)
            dispatch({
                type: 'GET_SP_LIST',
                value: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 获取商家列表
const getbuslist = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/business'
        fetch(url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'GET_BUS_LIST',
                value: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//根据用户id去拿预定表
const getappoint = (id) => {
    return (dispatch) => {
        // console.log(id)
        let url = 'http://www.h5weixin.club:1024/api/appointment'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // console.log(res);
            dispatch({
                type: 'GET_YD_LIST',
                data: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//根据用户id去拿收藏列表
const getcollect = (id) => {
    return (dispatch) => {
        // console.log(id)
        let url = 'http://www.h5weixin.club:1024/api/collection'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // console.log(res);
            dispatch({
                type: 'GET_SC_LIST',
                data: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//根据商家Id去拿商品列表
const getbusidlist = (id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/business/commodity'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'GET_SP_LIST',
                value: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

// const postsp = () => {
//     let url = 'http://www.h5weixin.club:1024/api/commodity'
//     fetch(url, {
//         method: 'POST',
//         mode: 'cors',
//         headers: {
//             'Content-Type': 'application/json'
//         },
//         body: JSON.stringify({
//             business_id: '11', commodity_temp_title: '杨乐一块一次', commodity_temp_descript: '来204找杨乐不包邮', commodity_temp_money: '100'
//         })
//     }).then((res) => {
//         return res.json()
//     }).then((res) => {
//         console.log(res)
//     })
// }

// 判断使用请求资源or本地资源
const getMsg = (props) => {//获取图片
    let data
    // console.log(props.login)
    if (props.login == []) {
        data = props.login[0]
    } else {
        data = JSON.parse(localStorage.getItem('user'))
    }
    // console.log(data)
    return data
}

const getBus = (props) => {
    let data
    props.business
        ? data = JSON.parse(localStorage.getItem('business'))
        : data = props.business
    return data
}

// 根据用户和商品id去删除预定
const delyuding = (user_id, commodity_id) => {
    // console.log(user_id, commodity_id)
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/appointment`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`, user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'DEL_YD',
                index: commodity_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

// 根据用户ID和商品ID获得评论
// commentInter
const getcomment = (user_id, commodity_id) => {
    console.log(user_id, commodity_id)
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/appointment`
        fetch(url, {
            method: '',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`, user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'DEL_YD',
                index: commodity_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

// 根据用户ID获取预定
const getyudinglist = (user_id) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/appointment`
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // errToast(res.msg)
            dispatch({
                type: 'GET_YD_LIST',
                data: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

// 添加预定
const addyuding = (user_id, commodity_id, commodity, time) => {
    console.log(time)
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/appointment`
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`,
                user_id: `${user_id}`,
                time: `${time}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'ADD_YD',
                index: commodity_id,
                value: commodity
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

// 根据用户和商品id去删除收藏
const delshoucang = (user_id, commodity_id) => {
    // console.log(user_id,commodity_id)
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/collection`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`, user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'DEL_SC',
                index: commodity_id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}

//根据用户和商品id去添加收藏
const addshoucang = (user_id, commodity_id, commodity) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/collection`
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`,
                user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'ADD_SC',
                index: commodity_id,
                value: commodity
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 得到收藏列表
const getshoucanglist = (user_id) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/collection`
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_id: `${user_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // errToast(res.msg)
            console.log(res.data)
            dispatch({
                type: 'GET_SC_LIST',
                data: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}




// 根据商品ID删除商品
const delsp = (id, business_id, business_password) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/commodity/`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${business_id}`, business_password: `${business_password}`, commodity_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'DEL_SP',
                index: id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 根据商家id去拿该商家待审核的商品
const getbusidlist_temp = (id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity/commodity_temp'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'GET_SP_LIST_TEMP',
                value: res.data
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 添加商品
const addsp_temp = (id, commodity_temp_title, commodity_temp_descript, commodity_temp_money, commodity_temp_time, commodity_temp_addres, File) => {
    const formData = new FormData()
    formData.append('business_id', id)
    formData.append('commodity_temp_title', commodity_temp_title)
    formData.append('commodity_temp_descript', commodity_temp_descript)
    formData.append('commodity_temp_money', commodity_temp_money)
    formData.append('commodity_temp_t', commodity_temp_time)
    formData.append('commodity_temp_addres', commodity_temp_addres)
    if (File[0].file == undefined) {
        formData.append('File', File[0].url)
    } else {
        formData.append('File', File[0].file)
    }
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/commodity'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            if (res.data != null) {
                dispatch({
                    type: 'ADD_SP_TEMP',
                    value: res.data
                })
            }
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 删除待审核商品，根据商家id，密码，商品id
const delsp_temp = (id, business_id, business_password) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/commodity/temp`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${business_id}`, business_password: `${business_password}`, commodity_temp_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            dispatch({
                type: 'DEL_SP_TEMP',
                index: id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 修改商品其实就是发布审核+删除上架商品
const modifysp = (id, business_id, business_password) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/commodity/`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${business_id}`, business_password: `${business_password}`, commodity_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast('修改成功')
            dispatch({
                type: 'DEL_SP',
                index: id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 修改待审核商品
const modifysp_temp = (id, business_id, business_password) => {
    return (dispatch) => {
        let url = `http://www.h5weixin.club:1024/api/commodity/temp`
        fetch(url, {
            method: 'DELETE',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                business_id: `${business_id}`, business_password: `${business_password}`, commodity_temp_id: `${id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast('修改成功')
            dispatch({
                type: 'DEL_SP_TEMP',
                index: id
            })
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 修改商家信息
const modifybus = (business_id, business_name, business_phone, business_email, business_time, business_describe, business_password, business_password_en, File, File1, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('business_name', business_name)
    formData.append('business_phone', business_phone)
    formData.append('business_email', business_email)
    formData.append('business_time', business_time)
    formData.append('business_describe', business_describe)
    formData.append('password', business_password)
    formData.append('password_confim', business_password_en)
    if (File[0].file == undefined) {
        formData.append('File', File[0].url)
    } else {
        console.log(File)
        formData.append('File', File[0].file)
    }
    if (File1[0].file == undefined) {
        formData.append('File1', File1[0].url)
    } else {
        formData.append('File1', File1[0].file)
    }
    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}

// 商家退出登录
const qutebus = (id) => {
    return (dispatch) => {
        localStorage.removeItem('user')
        localStorage.removeItem('identity')
        dispatch({
            type: 'DEL_UR',
            index: id
        })
    }
}

// 根据手机号获取用户信息，用来和商家的私聊
const getuserinfo = (phone, props) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/user/getinfo'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                user_phone: `${phone}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            let arr = props.user.filter((item) => {
                return item.user_id == res.data.user_id
            })
            if (arr.length == 0) {
                dispatch({
                    type: 'ADD_USER',
                    value: res.data
                })
            }
        })
    }

}
// const getinfo = async (res) => {
//     for (let i = 0; i < res.data.length; i++) {
//         res.data[i].user_name = await getuserinfo_name_pic(res.data[i].userco_id).user_name
//         res.data[i].user_pic = await getuserinfo_name_pic(res.data[i].userco_id).user_pic
//     }
//     return res
// }
// 根据商品id获取该商品的评论
const getcommlist = (commodity_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/comment/getlist'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodity_id: `${commodity_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'GET_COMM',
                value: res.data
            })
        })
    }
}
// 评论获取用户user_name,user_pic
// const getuserinfo_name_pic = (user_id) => {
//     return new Promise((resolve, reject) => {
//         let url = 'http://www.h5weixin.club:1024/api/user/getinfo2'
//         fetch(url, {
//             method: 'POST',
//             mode: 'cors',
//             headers: {
//                 'Content-Type': 'application/json'
//             },
//             body: JSON.stringify({
//                 user_id: `${user_id}`
//             })
//         })
//             .then(res => res.json())
//             .then(res => resolve(res.data))
//     })
// }

const getdynamic = () => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic'
        fetch(url, {
            method: 'GET',
            mode: 'cors',

        })
            .then(res => res.json())
            .then(res => {
                dispatch({
                    type: 'GET_DY_LIST',
                    value: res.data
                })
            })
    }
}
const getcommlist_dynamic = (dynamic_id) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/comment/getlist2'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                dynamic_id: `${dynamic_id}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            // console.log(res)
            dispatch({
                type: 'GET_COMM',
                value: res.data
            })
        })
    }
}
// 动态评论
const send_comment_dynamic = (dynamic_id, user_id, text, time) => {
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/comment/'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                dynamicco_id: `${dynamic_id}`, userco_id: `${user_id}`, comment_context: `${text}`, time: `${time}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'ADD_COMM',
                value: res.data
            })
        })
    }
}
// 商品评论
const send_comment_commodity = (commodity_id, user_id, text, time) => {
    console.log(commodity_id, user_id, text, time)
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/comment/'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                commodityco_id: `${commodity_id}`, userco_id: `${user_id}`, comment_context: `${text}`, time: `${time}`
            })
        }).then((res) => {
            return res.json()
        }).then((res) => {
            dispatch({
                type: 'ADD_COMM',
                value: res.data
            })
        })
    }
}

// 根据用户id拿他所发布的动态
const getuserdynamic = (user_id) => {
    console.log(user_id)
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic/dynamic2'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                userd_id: `${user_id}`
            })
        })
            .then(res => res.json())
            .then(res => {
                console.log(res.data)
                dispatch({
                    type: 'GET_DY_LIST',
                    value: res.data
                })
            })
    }
}
// 虚假的实名认证，真的要钱
const businessHeader = (business_id, business_header, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('business_header', business_header)

    return (dispatch) => {
        // console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/3'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}
// 发布动态
const postDynamic = (userd_id, dynamic_title, dynamic_context, File) => {
    const formData = new FormData()
    // let newFiles = new Array()
    formData.append('userd_id', userd_id)
    formData.append('dynamic_title', '标题没用')
    formData.append('dynamic_context', dynamic_context)
    // console.log(File)
    for (let i = 0; i < File.length; i++) {
        // newFiles[i] = File[i].file
        formData.append('File', File[i].file)
    }
    return (dispatch) => {
        let url = 'http://www.h5weixin.club:1024/api/dynamic/'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            body: formData
        }).then(res => { return res.json() })
        // .then(res => {
        //     console.log(res.data)
        //     dispatch({
        //         type: 'ADD_DY',
        //         value: res.data
        //     })
        // })
    }
}
// 删除动态
const Deldynamic = (dynamic_id) => {
    let url = 'http://www.h5weixin.club:1024/api/dynamic/'
    fetch(url, {
        method: 'DELETE',
        mode: 'cors',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            dynamic_id: `${dynamic_id}`
        })
    }).then((res) => {
        return res.json()
    })
}

const Delcomment = (comment_id) =>{
    console.log(comment_id)
    return (dispatch)=>{
    let url = 'http://www.h5weixin.club:1024/api/comment/'
    fetch(url, {
        method: 'DELETE',
        mode: 'cors',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            comment_id: `${comment_id}`
        })
    }).then((res) => {
        return res.json()
    }).then(res=>{
        dispatch({
            type:'DEL_COMM',
            value:res.data
        })
    })
    }
}
export {
    denglu, busdenglu, zhece, zhecebus, getsplist,
    getbuslist, getuserlist, getbusidlist,
    getMsg, delsp, getbusidlist_temp, getCounty,
    addsp_temp, delsp_temp, errToast, modifysp, modifysp_temp,
    getcollect, getappoint, modifybus, qutebus,
    delyuding, delshoucang, getshoucanglist, addshoucang,
    addyuding, getyudinglist, getBus, getuserinfo, getcommlist,
    getdynamic, getcommlist_dynamic, send_comment_dynamic, send_comment_commodity,
    getuserdynamic, businessHeader, postDynamic,Deldynamic,Delcomment
}